package com.bjesc_b2c.app.appraiset.widget.dialog;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.widget.BaseDialog;

/**
 * Created by yidong on 2017/6/26.
 */
public class UploadDialog extends BaseDialog implements View.OnClickListener{
    private TextView tv_progress;
    private TextView tv_failure;
    private TextView tv_cancel;
    private TextView tv_sure;
    private TextView tv_tip;
    private ProgressBar progressbar;
    private FailureUpload failureUpload;
    private Context mContext;

    public UploadDialog(Context context, FailureUpload failureUpload) {
        super(context);
        this.mContext = context;
        this.failureUpload = failureUpload;
        initViews();
    }

    private void initViews() {
        view = inflater.inflate(R.layout.dialog_upload, null);
        tv_progress = (TextView) view.findViewById(R.id.tv_progress);
        tv_failure = (TextView) view.findViewById(R.id.tv_failure);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        tv_tip = (TextView) view.findViewById(R.id.tv_analyse_tip);

        tv_cancel.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
        progressbar = (ProgressBar) view.findViewById(R.id.my_progressbar);
        setContentView(view);
    }

    public Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    int progress = (int) msg.obj;
                    progressbar.setProgress(progress);
                    tv_progress.setText(progress + "%");
                    break;
                case 2:
                    tv_failure.setVisibility(View.VISIBLE);
                    tv_sure.setText("继续上传");
                    String str = mContext.getResources().getString(R.string.upload_f);
                    String last = String.format(str,msg.obj);
                    tv_failure.setText(last);
                    break;
                case 3:
                    progressbar.setProgress(1);
                    tv_progress.setText(0 + "%");
                    tv_sure.setText("确定");
                    tv_failure.setVisibility(View.GONE);
                    break;
                case 4:
                    tv_tip.setText("上传成功！");
                    break;
            }

        }

    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_sure:
                if (failureUpload != null){
                    failureUpload.upload();
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }

    }

    public interface FailureUpload {
        void upload();
    }
}
